package ru.kreda.webapp.server;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;

import ru.kreda.webapp.shared.AppException;
import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.Action;
import net.customware.gwt.dispatch.shared.DispatchException;
import net.customware.gwt.dispatch.shared.Result;

public abstract class BaseActionHandler<A extends Action<R>, R extends Result> implements ActionHandler<A, R> {
	Logger logger = Logger.getLogger("dispatcher");

	@Override
	public final R execute(A action, ExecutionContext context) throws DispatchException, AppException {
		try {
			return executeAction(action, context);
		} catch (Exception e) {
			StringWriter writer = new StringWriter();
			e.printStackTrace(new PrintWriter(writer));
			String stackTrace = writer.toString();
			logger.info(stackTrace);
			AppException clientException = AppException.createClientException(e, stackTrace);
			throw clientException;
		}
	}

	protected abstract R executeAction(A action, ExecutionContext context) throws Exception;

	@Override
	public void rollback(A action, R result, ExecutionContext context) throws DispatchException {
		
	}

}
